﻿Imports System.IO
Imports System.IO.IsolatedStorage

Namespace ResourceManager

    Public Class IsolatedContentManager
        Inherits Microsoft.Xna.Framework.Content.ContentManager

        Public Property IsolatedStorageRootDirectory() As String

#If WINDOWS_PHONE Then
    Private m_fileStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
#ElseIf WINDOWS Then
        Private m_fileStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly()
#End If

        Public Sub New(serviceProvider As System.IServiceProvider, Optional ByVal fs As IsolatedStorageFile = Nothing)
            MyBase.New(serviceProvider)
            Me.m_fileStorage = If(fs, Me.m_fileStorage)
        End Sub

        Public Sub New(serviceProvider As System.IServiceProvider, rootDirectory As String, Optional ByVal fs As IsolatedStorageFile = Nothing)
            MyBase.New(serviceProvider, rootDirectory)
            Me.IsolatedStorageRootDirectory = rootDirectory
            Me.m_fileStorage = If(fs, Me.m_fileStorage)
        End Sub

        Public Function LoadSpriteFont(ByVal assetName As String) As FontObject
            Return New FontObject(assetName, assetName, MyBase.Load(Of SpriteFont)(assetName))
        End Function

        Public Function LoadTexture2D(ByVal assetName As String) As Texture2DObject
            Return New Texture2DObject(assetName, assetName, MyBase.Load(Of Texture2D)(assetName))
        End Function

        Public Function LoadAudio(ByVal assetName As String) As AudioObject
            Return New AudioObject(assetName, assetName, MyBase.Load(Of SoundEffect)(assetName))
        End Function

        Protected Overrides Function OpenStream(assetName As String) As System.IO.Stream
            Return Me.m_fileStorage.OpenFile(IO.Path.Combine(Me.IsolatedStorageRootDirectory, assetName), FileMode.Open)
        End Function

    End Class

End Namespace